package com.imaker.frame.image

import android.app.Activity
import android.content.Context
import android.os.Build
import android.widget.ImageView

/**
 * 图片库帮助类，兼容不同的图片加载库实例，便于扩展和替换
 */
class ImageHelper private constructor() {
    fun loadNetImg(
        context: Context?,
        url: String?,
        imageView: ImageView?,
        placeHolder: Int,
        errorHolder: Int
    ) {
        displayer!!.loadNetImg(context, url, imageView, placeHolder, errorHolder)
    }

    fun loadNetImg(context: Context?, url: String?, imageView: ImageView?) {
        displayer!!.loadNetImg(context, url, imageView)
    }

    fun loadNetImg(context: Context?, url: String?, iImageLoadResult: IImageLoadResult?) {
        displayer!!.loadNetImg(context, url, iImageLoadResult)
    }

    fun loadCircleImage(
        context: Context?,
        url: String?,
        imageView: ImageView?,
        placeholder: Int,
        errorholder: Int
    ) {
        displayer!!.loadCircleImage(context, url, imageView, placeholder, errorholder)
    }

    fun loadCircleImage(context: Context?, url: String?, imageView: ImageView?) {
        displayer!!.loadCircleImage(context, url, imageView)
    }

    fun loadCircleImage(context: Context?, url: Int, imageView: ImageView?) {
        displayer!!.loadCircleImage(context, url, imageView)
    }

    fun loadRoundCornerImg(
        context: Context?,
        url: String?,
        imageView: ImageView?,
        placeholder: Int,
        errorholder: Int,
        dp: Int
    ) {
        displayer!!.loadRoundCornerImg(context, url, imageView, placeholder, errorholder, dp)
    }

    fun loadRoundCornerImg(
        context: Context?,
        url: String?,
        imageView: ImageView?,
        placeholder: Int,
        errorholder: Int,
        leftTopRadius: Int,
        rightTopRadius: Int,
        leftBottomRadius: Int,
        rightBottomRadius: Int
    ) {
        displayer!!.loadRoundCornerImg(
            context,
            url,
            imageView,
            placeholder,
            errorholder,
            leftTopRadius,
            rightTopRadius,
            leftBottomRadius,
            rightBottomRadius
        )
    }

    fun loadRoundCornerImg(context: Context?, url: String?, imageView: ImageView?, dp: Int) {
        displayer!!.loadRoundCornerImg(context, url, imageView, dp)
    }

    fun loadRoundCornerImg(context: Context?, url: Int, imageView: ImageView?, dp: Int) {
        displayer!!.loadRoundCornerImg(context, url, imageView, dp)
    }

    fun loadResourceImage(context: Context?, resId: Int, imageView: ImageView?) {
        displayer!!.loadResourceImg(context, resId, imageView)
    }

    fun loadCircleImage(
        context: Context?,
        layoutId: Int,
        imageView: ImageView?,
        placeholder: Int,
        errorholder: Int
    ) {
        displayer!!.loadCircleImage(context, layoutId, imageView, placeholder, errorholder)
    }

    companion object {
        @Volatile
        var imageHelper: ImageHelper? = null
        var displayer: IImageLoaderDisplayer? = null
        val instance: ImageHelper?
            get() {
                var iPh = imageHelper
                if (iPh == null) {
                    synchronized(ImageHelper::class.java) {
                        iPh = imageHelper
                        if (iPh == null) {
                            iPh = ImageHelper()
                            imageHelper = iPh
                            displayer = GlideImageLoaderDisplayer()
                        }
                    }
                }
                return iPh
            }

        /**
         * 使用Glide加载图片时，先判断是否是有效的Context
         */
        fun isValidConntext(context: Context?): Boolean {
            if (context == null) {
                return false
            }
            if (context is Activity) {
                val activity = context as Activity?
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                    if (activity != null && activity.isDestroyed) {
                        return false
                    }
                }
            }
            return true
        }
    }
}